package com.lefeng.cockpit.admin.api.v1.datax;

import com.didiglobal.logi.security.common.PagingData;
import com.didiglobal.logi.security.common.PagingResult;
import com.didiglobal.logi.security.common.Result;
import com.lefeng.cockpit.common.bean.dto.datasource.DataSourceDTO;
import com.lefeng.cockpit.common.bean.po.datasource.DataSourcePO;
import com.lefeng.cockpit.common.bean.vo.datasource.DataSourceVO;
import com.lefeng.cockpit.datax.service.DataSourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author weifw
 */
@RestController
@RequestMapping("/api/v1/datax/datasource")
@Api(tags = "datax-数据源相关API")
public class DataSourceController {

    @Resource
    private DataSourceService dataSourceService;


    @PutMapping("/add")
    @ResponseBody
    @ApiOperation(value = "新建数据源接口", notes = "")
    public Result<Integer> add(@RequestBody DataSourceDTO dataSourceDTO) {
        return dataSourceService.addDataSource(dataSourceDTO);
    }

    @PostMapping("/clone")
    @ResponseBody
    @ApiOperation(value = "克隆数据源接口", notes = "")
    public Result<Integer> clone(@RequestBody DataSourceDTO dataSourceDTO) {
        return dataSourceService.cloneDataSource(dataSourceDTO);
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    @ApiOperation(value = "删除数据源接口", notes = "")
    public Result<Void> delete(@PathVariable(value = "id") Integer id) {
        return dataSourceService.delDataSource(id);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页数据源列表", notes = "分页和条件查询")
    public PagingResult<DataSourceVO> page(@RequestBody DataSourceDTO dataSourceDTO) {
        PagingData<DataSourceVO> configVO = dataSourceService.pagingDataSource(dataSourceDTO);
        return PagingResult.success(configVO);
    }

    @PostMapping("/all")
    @ApiOperation(value = "查询所有数据源", notes = "")
    public Result<List<DataSourcePO>> getAllDataSource(@RequestBody DataSourceDTO dataSourceDTO) {
        return dataSourceService.getAllDataSource(dataSourceDTO);
    }

    @PostMapping("/edit")
    @ResponseBody
    @ApiOperation(value = "编辑数据源接口", notes = "")
    public Result<Void> edit(@RequestBody DataSourceDTO dataSourceDTO) {
        return dataSourceService.editDataSource(dataSourceDTO);
    }

    @PostMapping("/test")
    @ApiOperation("测试数据源连通性")
    public Result<Boolean> dataSourceTest(@RequestBody DataSourceDTO dataSourceDTO) {
        return Result.build(dataSourceService.dataSourceTest(dataSourceDTO));
    }

    @PostMapping("/switch")
    @ResponseBody
    @ApiOperation(value = "数据源开关接口", notes = "")
    public Result<Void> switchConfig(@RequestBody DataSourceDTO dataSourceDTO) {
        return dataSourceService.switchDataSource(dataSourceDTO.getId(), dataSourceDTO.getStatus());
    }

    @GetMapping("/type-list")
    @ResponseBody
    @ApiOperation(value = "获取数据源的类型列表", notes = "")
    public Result<List<String>> groups() {
        return Result.buildSucc(dataSourceService.listDataSourceTypes());
    }


}